from django.db import models
from django.contrib import admin
from django.utils.timezone import now
from user.models import User,Address


#事件图片
class EventImg(models.Model):
    """
    事件图片
    """
    del_choices = (('0', '未删除'),('1', '已删除'))
    event_img =models.ImageField(upload_to='img/event', verbose_name='事件图片', help_text='事件图片（ImageField）', blank=True)
    event = models.ForeignKey('Event',verbose_name='事件',help_text='事件（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL)
    created_by = models.ForeignKey(User,verbose_name='创建人ID',help_text='创建人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='event_img_createdby')
    created_date = models.DateTimeField(verbose_name='创建时间', help_text='创建时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User,verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='event_img_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除', help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')
    class Meta:
        ordering = ['-id']
        verbose_name = '事件图片'
        verbose_name_plural = verbose_name
    def __str__(self):
        return str(self.event_img)
@admin.register(EventImg)
class EventImgAdminModel(admin.ModelAdmin):
    list_per_page = 20
    list_display = ('id', 'event_img', 'created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')

#事件类别
class EventType(models.Model):
    """事件类别"""
    del_choices = (('0', '未删除'), ('1', '已删除'))
    name = models.CharField(max_length=20, verbose_name='类型名称', help_text='类型名称（CharField）')
    created_by = models.ForeignKey(User, verbose_name='创建人ID',help_text='创建人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='eventtype_createdby')
    created_date = models.DateTimeField(verbose_name='创建时间', help_text='创建时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User, verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='eventtype_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除',help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')
    class Meta:
        ordering = ['-id']
        verbose_name = '事件类别'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name
@admin.register(EventType)
class EventTypeAdminModel(admin.ModelAdmin):
    """
    事件类别
    """
    list_per_page = 20
    list_display = ('id','name','created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')

# 事件
class Event(models.Model):
    """
    需求事件
    """
    del_choices = (('0', '未删除'), ('1', '已删除'))
    state_choices = (('0', '发起'), ('1', '进行中'), ('2', '已完成'))

    name = models.CharField(max_length=20, verbose_name='标题', help_text='标题（CharField）')
    desc = models.TextField(verbose_name='详情', help_text='详情（TextField）', blank=True)
    eventType = models.ForeignKey('EventType',verbose_name='事件类别',help_text='事件类别（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL)

    start_date = models.DateTimeField(verbose_name='开始时间', help_text='开始时间（DateTimeField）', null=True, blank=True)
    end_date = models.DateTimeField(verbose_name='结束时间', help_text='结束时间（DateTimeField）', null=True, blank=True)
    address = models.ForeignKey(Address,verbose_name='地点',help_text='地点（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL)
    created_by = models.ForeignKey(User, verbose_name='发起人ID',help_text='发起人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='event_createdby')
    state = models.CharField(max_length=1, verbose_name='当前状态',help_text='当前状态（CharField，可选值：0，1，2）', choices=state_choices, default='0')
    emissary = models.ForeignKey(User, verbose_name='接手人ID',help_text='接手人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='emissary')
    
    # 因为有些金币可能拥有特定的故事，拥有者可能有不想将其使用出去的时候，所以这里的金币用外联表来关联选择，拥有者可以选择使用哪一些金币，然后在事件表单记录金额即可
    goldnum = models.CharField(max_length=200, verbose_name='佣金', help_text='佣金（CharField）')

    created_date = models.DateTimeField(verbose_name='发起时间', help_text='发起时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User, verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='event_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除',help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')

    class Meta:
        ordering = ['-id']
        verbose_name = '事件'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name
@admin.register(Event)
class EventAdminModel(admin.ModelAdmin):
    """
    事件
    """
    list_per_page = 20
    list_display = ('id','name','desc','eventType','start_date','end_date','address','state','emissary','goldnum','created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')


# 评论
class Comment(models.Model):
    """
    需求事件评论
    """
    del_choices = (('0', '未删除'), ('1', '已删除'))

    event = models.ForeignKey('Event',verbose_name='事件',help_text='事件（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL)
    news = models.CharField(max_length=120, verbose_name='评论内容', help_text='评论内容（CharField）')
    order = models.PositiveSmallIntegerField(verbose_name='楼层',help_text='楼层（ForeignKey）', null=True, blank=True)

    created_by = models.ForeignKey(User, verbose_name='评论人ID',help_text='评论人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='comment_createdby')
    created_date = models.DateTimeField(verbose_name='评论时间', help_text='评论时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User, verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='comment_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除',help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')

    class Meta:
        ordering = ['id']
        verbose_name = '事件评论，跟帖'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.news
@admin.register(Comment)
class CommentAdminModel(admin.ModelAdmin):
    """
    事件评论
    """
    list_per_page = 20
    list_display = ('id','event','news','order','created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')

